package com.gdziarmaga.webshop.client.jsf;

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.xml.ws.soap.SOAPFaultException;

import org.springframework.beans.factory.annotation.Autowired;

import com.gdziarmaga.webshop.utils.springcontext.AppContext;
import com.gdziarmaga.webshopwebservices.BookByISBNResponse.BookDetail;
import com.gdziarmaga.webshopwebservices.ListReviewsByBookISBNResponse;
import com.gdziarmaga.webshopwebservices.WebShopWebServices;

@ManagedBean(name = "clientBean")
@ViewScoped
public class ClientBean {

	public boolean isRendered() {
		return rendered;
	}

	public void setRendered(boolean rendered) {
		this.rendered = rendered;
	}

	@Autowired
	private WebShopWebServices webShopService;

	private String isbn;

	private BookDetail book;

	private List<ListReviewsByBookISBNResponse.ReviewDetail> reviews;

	public List<ListReviewsByBookISBNResponse.ReviewDetail> getReviews() {
		return reviews;
	}

	public void setReviews(
			List<ListReviewsByBookISBNResponse.ReviewDetail> reviews) {
		this.reviews = reviews;
	}

	public BookDetail getBook() {
		return book;
	}

	public void setBook(BookDetail book) {
		this.book = book;
	}

	private boolean rendered = false;

	public String getIsbn() {
		return isbn;
	}

	public void setIsbn(String isbn) {
		this.isbn = isbn;
	}

	public void action() {
		webShopService = (WebShopWebServices) AppContext
				.getApplicationContext().getBean("webShopServiceClient");
		try{
		reviews = webShopService.listReviewsByBookISBN(isbn);
		book = webShopService.bookByISBN(isbn);
		rendered=true;
		}catch(Exception e){
			if (e instanceof SOAPFaultException){
				rendered=false;
			}
		}
	}
}
